package cn.platform.controller;

import cn.platform.request.AttendanceQueryDTO;
import cn.platform.request.AttendanceRecordDTO;
import cn.platform.response.AttendanceVO;
import cn.platform.response.PageResult;
import cn.platform.response.Result;
import cn.platform.service.AttendanceService;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/attendance")
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;

    @PostMapping("/record")
    public Result recordAttendance(@RequestBody AttendanceRecordDTO attendanceRecord) throws JsonProcessingException {
        log.info("学生发送打卡{}",attendanceRecord);
        attendanceService.recordAttendance(attendanceRecord);
        return Result.success("打卡成功");
    }

    @GetMapping("/records")
    public Result page(AttendanceQueryDTO attendanceQueryDTO){
        log.info("分页查询考勤信息{}",attendanceQueryDTO);
        PageResult<AttendanceVO> pageResult=attendanceService.page(attendanceQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping("/record/{id}")
    public Result deleteRecord(@PathVariable Integer id){
        log.info("删除单条考勤记录{}",id);
        attendanceService.deleteRecord(id);
        return Result.success();
    }

    @DeleteMapping("/records")
    public Result batchDeleteRecords(@RequestBody List<Integer> ids){
        log.info("批量删除考勤信息{}",ids);
        attendanceService.deleteBatchRecords(ids);
        return Result.success();
    }
}
